package com.warmheart.core.combine.oper;

import java.util.Iterator;
import java.util.List;
import java.util.function.Supplier;

import javax.validation.constraints.NotNull;

import org.apache.commons.lang3.StringUtils;

import com.warmheart.core.constant.Globals;
import com.warmheart.core.cors.builder.CoRsBuilder;
import com.warmheart.core.rs.builder.RsBuilder;
import com.warmheart.core.rs.enums.FieldTypeEnum;
import com.warmheart.core.rs.model.SelectedModel;
import com.warmheart.core.rs.oper.Rs;

/**
 * 
 * @ClassName: Select
 * @Description: 该类用作对联合查询字段进行处理
 * @author ZhangJianyang
 * @date 2025-06-16 09:37:02
 *
 */
public class Select {

    private final Combine combine;

    public Select(Combine combine) {
        this.combine = combine;
    }

    public <T extends CoRsBuilder<?, E>, E extends RsBuilder<T>> Select first(@NotNull Supplier<Rs<T, E>> supplier,
            String... aliasNames) {
        List<SelectedModel> selectList = this.combine.mainT().getJoinModel().getSelectList();
        Rs<T, E> rs = supplier.get();
        String pAlias = this.combine.aliasName(rs.core());
        selectList.clear();
        this.combine.mainT().getJoinModel().setMainAllSelectFlag(false);
        String aliasName = rs.fetchProp();
        if (aliasNames != null && aliasNames.length > 0 && StringUtils.isNotBlank(aliasNames[0])) {
            aliasName = aliasNames[0];
        }
        selectList.add(new SelectedModel(FieldTypeEnum.MULTIPLE_FIRST, pAlias + Globals.DOT + rs.fetchField(),
                aliasName));
        return this;
    }

    /**
     * 
     * @Title: next
     * @Description: 多字段查询后续字段添加
     * @author ZhangJianyang
     * @param supplier
     * @param aliasNames
     * @return
     */
    public <T extends CoRsBuilder<?, E>, E extends RsBuilder<T>> Select next(@NotNull Supplier<Rs<T, E>> supplier,
            String... aliasNames) {
        List<SelectedModel> selectList = this.combine.mainT().getJoinModel().getSelectList();
        Rs<T, E> rs = supplier.get();
        String pAlias = this.combine.aliasName(rs.core());
        Iterator<SelectedModel> iterator = selectList.iterator();
        while (iterator.hasNext()) {
            SelectedModel element = iterator.next();
            if ((pAlias + Globals.DOT + rs.fetchField()).equals(element.getFieldName())) {
                iterator.remove();
                // 由于每次添加都会进行字段检查，所有这里最多只可能有一个，可以直接中断循环
                break;
            }
        }
        String aliasName = rs.fetchProp();
        if (aliasNames != null && aliasNames.length > 0 && StringUtils.isNotBlank(aliasNames[0])) {
            aliasName = aliasNames[0];
        }
        selectList
                .add(new SelectedModel(FieldTypeEnum.MULTIPLE_NEXT, pAlias + Globals.DOT + rs.fetchField(), aliasName));
        return this;
    }

    public void clear() {
        List<SelectedModel> selectList = this.combine.mainT().getJoinModel().getSelectList();
        selectList.clear();
    }

    public Combine end() {
        return this.combine;
    }
}
